package com.shanzmoo.merchant.server.api.domain;

import cn.hutool.core.util.StrUtil;
import com.shanzmoo.base.constant.e.LoginUserType;
import com.shanzmoo.db.module.member.entity.MemberInfoEntity;
import com.shanzmoo.db.module.member.entity.MemberMerchantEntity;
import com.shanzmoo.db.module.member.entity.MemberMerchantTmpEntity;
import com.shanzmoo.merchant.server.api.util.BCryptUtils;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;

/**
 * 认证用户
 * @author: Gzhao 2020/11/9
 */
@Data
@Accessors(chain = true)
public class AuthUser implements UserDetails {

    /**
     * 用户id
     */
    private Integer userId;

    /**
     * 手机号
     */
    private String mobile;
    /**
     * 微信openId
     */
    private String wxOpenId;
    /**
     * 支付宝openId
     */
    private String zfbOpenId;

    /**
     * 用户状态
     */
    private Boolean status;

    /**
     * 用户类型
     */
    private LoginUserType userType;



    public static AuthUser create(MemberInfoEntity userEntity) {
        if (null == userEntity){
            return null;
        }
        return  new AuthUser()
                .setUserId(userEntity.getId())
                .setMobile(userEntity.getMobile())
                .setWxOpenId(userEntity.getWxOpenId())
                .setZfbOpenId(userEntity.getZfbOpenId())
                .setStatus(userEntity.getStatus()==1)
                .setUserType(LoginUserType.PT_FORMAL);
    }
    public static AuthUser create(MemberMerchantTmpEntity userEntity) {
        if (null == userEntity){
            return null;
        }
        String loginName;
        if(StrUtil.isNotBlank(userEntity.getOpenId())){
            loginName = userEntity.getOpenId();
        }else if(StrUtil.isNotBlank(userEntity.getBuyerId())){
            loginName = userEntity.getBuyerId();
        }else{
            return null;
        }
        return  new AuthUser()
                .setUserId(userEntity.getId())
                .setMobile(loginName)
                .setWxOpenId(userEntity.getOpenId())
                .setZfbOpenId(userEntity.getBuyerId())
                .setStatus(true)
                .setUserType(LoginUserType.SH_TEMP);
    }
    public static AuthUser create(MemberMerchantEntity userEntity) {
        if (null == userEntity){
            return null;
        }
        return  new AuthUser()
                .setUserId(userEntity.getId())
                .setMobile(userEntity.getMobile())
                .setWxOpenId(userEntity.getOpenId())
                .setZfbOpenId(userEntity.getBuyerId())
                .setStatus(userEntity.getStatus()==1)
                .setUserType(LoginUserType.SH_HUIYUAN);
    }

    public static UserDetails createObj(Object user) {
        if(user instanceof MemberInfoEntity){
            return create((MemberInfoEntity)user);
        }else if(user instanceof MemberMerchantTmpEntity){
            return create((MemberMerchantTmpEntity)user);
        }else if(user instanceof MemberMerchantEntity){
            return create((MemberMerchantEntity)user);
        }else{
            return null;
        }
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return BCryptUtils.encodePassword("1");
    }

    @Override
    public String getUsername() {
        return userType.toString()+"___"+mobile;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
